myApp.controller('addClass', ['$scope', '$http', '$class', function($scope, $http, $class){
	$scope.className = "";
	$scope.classDescription = "无";
	$scope.addClass = function() {
		if($scope.className === "") {
			alert("请填写需要添加的类型名称！");
		}else {
			var classData = {"cla.name": $scope.className, "cla.description": $scope.classDescription};

			var postCfg = {
				headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
				transformRequest: function (data) {
					return $.param(data);
				}
			};

			$http.post('addClass', classData, postCfg).then(function successCallback(response) {
				alert("已添加成功！");
				$scope.className = "";
				$scope.classDescription = "无";

				var promise = $class.loadAllClass();
				promise.then(function(data) {
					$class.allClass = data;
				}, function(n) {
					$class.allClass = [{"name": "数据加载出错！"}];
				});
				console.log("ok：" + response);
			}, function errorCallback(response) {
				console.log("fail：" + response);
			});
		}
	};
}]);

myApp.controller('deleteClass', ['$scope', '$http', '$class', function($scope, $http, $class){
	
	$scope.classId = "";
	$scope.deleteClass = function() {
		if ($scope.classId === "" || $scope.classId === null) {
			alert("请选择需要删除的类型！");
		}else {
			var classData = {"cla.classId": $scope.classId};

			var postCfg = {
				headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
				transformRequest: function (data) {
					return $.param(data);
				}
			};

			$http.post('deleteClass', classData, postCfg).then(function successCallback(response) {
				alert("已删除成功！");
				$scope.classId = "";

				var promise = $class.loadAllClass();
				promise.then(function(data) {
					$class.allClass = data;
				}, function(n) {
					$class.allClass = [{"name": "数据加载出错！"}];
				});
				console.log("ok：" + response);
			}, function errorCallback(response) {
				console.log("fail：" + response);
			});
		}
		
	};
}]);

myApp.controller('modifyClass', ['$scope', '$http', '$class', function($scope, $http, $class){
	
	$scope.classId = "";
	$scope.className = "";
	$scope.classDescription = "无";
	$scope.modifyClass = function() {
		if($scope.classId === "" || $scope.classId === null) {
			alert("请选择需要修改的类型！");
		}else if ($scope.className === "") {
			alert("请填写类型名称！");
		}else {
			var classData = {"cla.classId": $scope.classId, "cla.name": $scope.className, "cla.description": $scope.classDescription};
			var postCfg = {
				headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
				transformRequest: function (data) {
					return $.param(data);
				}
			};

			$http.post('modifyClass', classData, postCfg).then(function successCallback(response) {
				alert("已修改成功！");
				$scope.classId = "";
				$scope.className = "";
				$scope.classDescription = "无";

				var promise = $class.loadAllClass();
				promise.then(function(data) {
					$class.allClass = data;
				}, function(n) {
					$class.allClass = [{"name": "数据加载出错！"}];
				});
				console.log("ok：" + response);
			}, function errorCallback(response) {
				console.log("fail：" + response);
			});
		}
		
	};
}]);